package com.hb.game.pk.ui.activity.mine;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hb.game.pk.R;
import com.hb.game.pk.adpter.ChooseMoneyAdpter;
import com.hb.game.pk.api.Host;
import com.hb.game.pk.api.RedBag;
import com.hb.game.pk.base.BaseActivity;
import com.hb.game.pk.beans.UrlBean;
import com.hb.game.pk.event.MoneyChangeEvent;
import com.hb.game.pk.event.RefreshUserInfoEvent;
import com.hb.game.pk.network.RetrofitCreateHelper;
import com.hb.game.pk.utils.ToastUtils;
import com.hb.game.pk.widgets.titlebar.TitleBar;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RechargeActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.btn_ok)
    Button btnOk;
    @BindView(R.id.rv_list)
    RecyclerView rv_list;
    private Map<String, TextView> click = new HashMap<>();
    int clickPosition = -1;
    List<String> moneyList;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_withdraw_cash;
    }

    @Override
    protected int getRootViewId() {
        return R.id.layout_root;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return 0;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        titleBar.getLlLeft().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RechargeActivity.this.finish();
                EventBus.getDefault().post(new RefreshUserInfoEvent());
            }
        });
        rv_list.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
        mAdapter = new ChooseMoneyAdpter(R.layout.item_money_list);
        moneyList = new ArrayList<>();
        moneyList.add("50.01");
        moneyList.add("100.45");
        moneyList.add("158.08");
        moneyList.add("200.01");
        moneyList.add("458.07");
        moneyList.add("968.01");
        moneyList.add("1378.05");
        moneyList.add("2000.02");
        mAdapter.setNewData(moneyList);
        rv_list.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Iterator<Map.Entry<String, TextView>> it = click.entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry<String, TextView> itEntry = it.next();
                    TextView itValue = itEntry.getValue();
                    itValue.setBackgroundResource(R.drawable.bg_btn_small_kong_radius);
                }
                TextView textView = (TextView) view.findViewById(R.id.tv_money);
                click.put("text" + position, textView);
                textView.setBackgroundResource(R.drawable.bg_btn_small_radius);
                clickPosition = position;
            }
        });
    }


    @OnClick(R.id.btn_ok)
    public void onViewClicked() {
        String moneyStr = moneyList.get(clickPosition);
        Call<UrlBean> call = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).txMoney(moneyStr);
        call.enqueue(new Callback<UrlBean>() {
            @Override
            public void onResponse(Call<UrlBean> call, Response<UrlBean> response) {
                UrlBean body = response.body();
                if (body == null) {
                    ToastUtils.showToast("请求错误");
                    return;
                }

                if (body.getStatus() != 1) {
                    ToastUtils.showToast(body.getInfo());
                    return;
                }
                Uri uri = Uri.parse(body.getData().getUrl());
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
            }

            @Override
            public void onFailure(Call<UrlBean> call, Throwable t) {
                ToastUtils.showToast("网络请求错误");
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().post(new MoneyChangeEvent());
    }
}
